import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { setUserInfo, getUserInfo, setDepartment, getDepartment } from '@/utils/localStorageData'
import { depaList } from "@/api/authorization";
const getDefaultState = () => {
    return {
        token: getToken(),
        userInfo: getUserInfo(),
        department: getDepartment(),
        name: '',
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_USERINFO: (state, userInfo) => {
        state.userInfo = userInfo
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_DEPARTMENT: (state, department) => {
        state.department = department
    }
}

const actions = {
    // user login
    login({ commit }, userInfo) {
        const { phone, password } = userInfo
        return new Promise((resolve, reject) => {
            login({ phone: phone.trim(), password: password }).then(response => {
                if (response.code === 200) {
                    const { data } = response
                    commit('SET_TOKEN', data)
                    setToken(data)
                    resolve()
                } else {
                    console.log(response.msg)
                    reject(response.msg)
                }
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    getInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            getInfo().then(response => {
                if (response.code === 200) {
                    const { data } = response
                    commit('SET_USERINFO', data)
                    commit('SET_NAME', data.user.user_name)
                    setUserInfo(data)
                    resolve(data)
                } else {
                    reject(response.msg)
                }

            }).catch(error => {
                reject(error)
            })
        })
    },

    // user logout
    logout({ commit, state }) {
        return new Promise((resolve, reject) => {
            logout(state.token).then(() => {
                removeToken() // must remove  token  first
                resetRouter()
                commit('RESET_STATE')
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    //部门
    depaList({ commit }) {
        return new Promise((resolve, reject) => {
            let data = {
                department_status: 1,
                department_name: ""
            };
            depaList(data).then(response => {
                if (response.code === 200) {
                    const { data } = response
                    commit('SET_DEPARTMENT', data)
                    setDepartment(data)
                    resolve(data)
                } else {
                    reject(response.msg)
                }
            }).catch(error => {
                reject(error)
            })
        })
    },

    // remove token
    resetToken({ commit }) {
        return new Promise(resolve => {
            removeToken() // must remove  token  first
            commit('RESET_STATE')
            resolve()
        })
    },
    // remove token
    resetName({ commit }) {
        return new Promise(resolve => {
            commit('SET_NAME')
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}